package com.rq.rvlibrary;

import android.content.Context;
import android.util.SparseArray;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

/**
 * @author RaoQian
 * @time 2025/8/6 10:52
 */
public class LoopAdapter extends BaseAdapter {
    public LoopAdapter(Context context, int itemLayoutId) {
        super(context, itemLayoutId);
    }

    public LoopAdapter(Context context, int itemLayoutId, Class baseViewHolderClass) {
        super(context, itemLayoutId, baseViewHolderClass);
    }

    public LoopAdapter(Context context, int itemLayoutId, Class baseViewHolderClass, Object obj) {
        super(context, itemLayoutId, baseViewHolderClass, obj);
    }

    public LoopAdapter(Context context, NestingBuilder builder) {
        super(context, builder);
    }

    public LoopAdapter(Context context, Class baseViewHolderClass, Object obj) {
        super(context, baseViewHolderClass, obj);
    }

    public LoopAdapter(Context context, @NonNull SparseArray maps, Object... innerClassContext) {
        super(context, maps, innerClassContext);
    }

    @Override
    public Object getDataItem(int position) {
        if (getDataSize() == 0) return null;
        return super.getDataItem(position % getDataSize());
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    protected void onBindFootHolder(BaseAdapter adapter, BaseViewHolder holder, int position, Object o) {
        try {

            int realPosition = getDataSize() > 0 ? position % getDataSize() : position;
            onBindEasyHolder(adapter, holder, realPosition, getDataItem(realPosition));
        } catch (Exception e) {
            int realPosition = getDataSize() > 0 ? position % getDataSize() : position;
            LOG.e("LoopAdapter", "onBindFootHolder.position:" + position);
            LOG.e("LoopAdapter", "onBindFootHolder.realPosition:" + realPosition);
            LOG.e("LoopAdapter", "onBindFootHolder.getDataItem:" + getDataItem(realPosition));
        }
    }

    @Override
    protected Object getObjectWithRealPosition(int position) {
        if (getDataSize() > 0) {
            return super.getObjectWithRealPosition(position % getDataSize());
        } else {
            return super.getObjectWithRealPosition(position);
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (getDataSize() > 0) {
            return super.onCreateViewHolder(parent, viewType % getDataSize());
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }
}
